/**
 * Created by EEO on 2017/6/9.
 */
~function (pro) {
    //->format:解析一个URL地址问号传递参数的值
    function format() {
        var reg = /([^?=&#]+)=([^?=&#]+)/g,
            obj = {};
        this.replace(reg, function () {
            obj[arguments[1]] = arguments[2];
        });
        return obj;
    }

    pro.format = format;
}(String.prototype);
var teacherRender = (function () {
    var id = 0;
    function bindHTML(data) {
        var str = '';
        for (var i = 0, len = data.data.list.length; i < len; i++) {
            var cur = data.data.list[i];
            if(cur.masterinfo!=null){
                var name = cur.masterinfo.lastname
            }
            str += '     <dl>\
             <dt><img style="width: 260px;height: 250px;" src="'+cur.cover+'"/></dt>\
             <dd>\
             <p class="home_courser_one">' + cur.title + '</p>\
             <p class="home_courser_two"><span class="home_courser_two_span">教师:<a>' + name + '</a></span><i class="courser_yellow">' + cur.price + '</i><p>\
             <p class="home_courser_three" style="clear: both;margin-left:14px;"><span>浏览:<a class="browse">' + cur.visited + '</a>　购买:<a >' + cur.sold + '</a></span><button class="by">购买</button><p>\
             </dd>\
             </dl>'
        }
        $('#courselist').html(str);
    }
    function UrlSearch() {
        var str = window.location.href.format(),
            id = str['id'];
        if (id === undefined) {
            id = 3
        }
        id = Number(id);
        $.ajax({
            url: '/api/teachers/type?id=' + id,
            dataType: 'json',
            success: function (data) {
                var ary = [];
                $.each(data.data.list, function (index, item) {
                    if (item.teacheruid === id) {
                        ary.push(item)
                    }
                });
                teacherInfo(ary);
                /*  if (id === undefined) {
                 id = 1;
                 for (var i = 0; i < data.length; i++) {
                 var cur = data[i];
                 if (cur.id != id) {
                 data.splice(i, 1);
                 i--;
                 }
                 }
                 teacherInfo(data);
                 } else {
                 for (var i = 0; i < data.length; i++) {
                 var cur = data[i];
                 if (cur.id != id) {
                 data.splice(i, 1);
                 i--;
                 }
                 }
                 teacherInfo(data)
                 }*/
            }
        });
    }
    function teacherInfo(data) {
        var str = '';
        for (var i = 0, len = data.length; i < len; i++) {
            var cur = data[i];
            str += ' <dl>\
                <dt><img src="img/student.png"/></dt>\
                <dd>\
                <h4>' + cur.lastname + '</h4>\
            <p>' + cur.introduce + '</p>\
            <div>' + cur.introduce + '</div>\
            </dd>\
            </dl>'
        }
        $('#teacherInfo').html(str)

    }

    return {
        init: function () {
            $.ajax({
                url: '/api/teachers/type/id=' + id,
                success: function (data) {
                    UrlSearch();
                }
            });
            $.ajax({
                url: '/api/products',
                success: function (data) {
                    bindHTML(data);
                }
            })
        }
    }
})();
teacherRender.init();